package com.shop.controller;

import com.shop.base.BaseController;
import com.shop.entity.Cart;
import com.shop.result.ResultVO;
import com.shop.service.CartService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/cart")
public class CartController extends BaseController {

    private final CartService cartService;

    /**
     * 获取购物车列表
     *
     * @param pageNum
     * @param pageSize
     * @param userId
     * @return
     */
    @RequestMapping("/listCart")
    public List<Cart> listCart(@RequestParam("pageNo") Integer pageNum, @RequestParam("pageSize")Integer pageSize,@RequestParam("userId") Integer userId) {
        return cartService.listCart(pageNum, pageSize, userId);
    }

    @GetMapping("/listCartMappings")
    public List<Cart> listCartMappings(@RequestParam("id") Integer id){
        return cartService.listCartMappings(id);
    }

    /**
     * 添加购物车
     *
     * @param cart
     */
    @PostMapping("/addCart")
    public ResultVO<Cart> addCart(@RequestBody Cart cart) {
        cartService.addCart(cart);
        return success(cart);
    }

    /**
     * 修改购物车数量
     *
     * @param id
     * @param quantity
     * @return
     */
    @RequestMapping("/updateCartNum")
    public int updateCartNum(@RequestParam("id")Integer id, @RequestParam("quantity")int quantity) {
        System.out.println("进入updateCartNum方法");
        System.out.println("quantity:"+quantity);

        return cartService.updateCartNum(id, quantity);
    }

    /**
     * 批量删除购物车
     *
     * @param userId
     * @param productIds
     */
    @RequestMapping("/deleteCartBatch")
    public void deleteCartBatch(Integer userId, List<Integer> productIds) {
        cartService.deleteCartBatch(userId, productIds);
    }
    /**
     * 删除购物车
     *
     * @param id
     */
    @RequestMapping("/deleteCart")
    public void deleteCart(@RequestParam("id") Integer id) {
        cartService.deleteCart(id);
    }
}
